import { householdApplyUrl, householdDetailUrl } from '../../../../api/individual'
import { post, uploadFile, initUploadFileListKey, getMessageAuth } from '../../../../utils/index'
import WxValidate from '../../../../utils/WxValidate'

Page({

  data: {
    form: {
      idCardImgFrontList: [],
      idCardImgBackList: [],
      householdImgFirstList: [],
      householdImgPersonList: []
    }
  },
  async loadData () { // 获取申请详情
    const { data } = await post(householdDetailUrl)
    if (data.id) {
      data.img = `../../../../assets/img/${data.status ? 'checkFail' : 'checking'}_img.png`
      data.title = data.status ? '办理失败' : '等待处理'
      data.subTitle = data.status ? data.msg : '已提交审核，等待工作人员处理'
      data.idCardImgFrontList = [{ url: data.idCardImgFrontAddress, name: '身份证人像面' }]
      data.idCardImgBackList = [{ url: data.idCardImgBackAddress, name: '身份证国徽面' }]
      data.householdImgFirstList = [{ url: data.householdImgFirstAddress, name: '传户口本首页' }]
      data.householdImgPersonList = [{ url: data.householdImgPersonAddress, name: '户口本个人页' }]
      this.setData({ form: data })
    }
  },
  async submit () { // 申请
    const { form } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      wx.showToast({ title: '信息填写有误，请检查', icon: 'none' })
      return
    }
    await post(householdApplyUrl, form)
    this.loadData()
    getMessageAuth()
  },
  edit () { // 重新编辑
    this.setData({ 'form.status': '' })
  },
  async idCardImgFrontRead ({ detail }) { // 身份证人像面上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ 'form.idCardImgFrontList': [data] })
    this.data.form.idCardImgFront = data.link
    this.data.form.idCardImgFrontId = data.id
  },
  idCardImgFrontDel () { // 身份证人像面删除
    this.data.form.idCardImgFront = ''
    this.data.form.idCardImgFrontId = ''
    this.setData({ 'form.idCardImgFrontList': [] })
  },
  async idCardImgBackRead ({ detail }) { // 身份证国徽面上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ 'form.idCardImgBackList': [data] })
    this.data.form.idCardImgBack = data.link
    this.data.form.idCardImgBackId = data.id
  },
  idCardImgBackDel () { // 身份证国徽面删除
    this.data.form.idCardImgBack = ''
    this.data.form.idCardImgBackId = ''
    this.setData({ 'form.idCardImgBackList': [] })
  },
  async householdImgFirstRead ({ detail }) { // 传户口本首页上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ 'form.householdImgFirstList': [data] })
    this.data.form.householdImgFirst = data.link
    this.data.form.householdImgFirstId = data.id
  },
  householdImgFirstDel () { // 传户口本首页删除
    this.data.form.householdImgFirst = ''
    this.data.form.householdImgFirstId = ''
    this.setData({ 'form.householdImgFirstList': [] })
  },
  async householdImgPersonRead ({ detail }) { // 户口本个人页上传
    const { file } = detail
    let { data } = await uploadFile(file.path)
    data = initUploadFileListKey(data)
    this.setData({ 'form.householdImgPersonList': [data] })
    this.data.form.householdImgPerson = data.link
    this.data.form.householdImgPersonId = data.id
  },
  householdImgPersonDel () { // 户口本个人页删除
    this.data.form.householdImgPerson = ''
    this.data.form.householdImgPersonId = ''
    this.setData({ 'form.householdImgPersonList': [] })
  },
  initValidator () { // 初始化验证器
    const rules = {
      idCardImgFrontId: { required: true },
      idCardImgBackId: { required: true },
      householdImgFirstId: { required: true },
      householdImgPersonId: { required: true }
    }
    const messages = {
      idCardImgFrontId: { required: '请上传身份证人像面' },
      idCardImgBackId: { required: '请上传身份证国徽面' },
      householdImgFirstId: { required: '请上传户口本首页' },
      householdImgPersonId: { required: '请上传户口本个人页' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  onLoad () {
    this.loadData()
    this.initValidator()
  },
  onPullDownRefresh () {
    const { status } = this.data.form
    if (status === 0 || status === 2) {
      this.loadData()
    }
  }
})